package com.holike.crm.fragment.bank;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.holike.crm.R;
import com.holike.crm.activity.credit.BillDetailListActivity;
import com.holike.crm.base.MyFragment;
import com.holike.crm.bean.BillListBean;
import com.holike.crm.dialog.CalendarPickerDialog;
import com.holike.crm.presenter.fragment.BillPresenter;
import com.holike.crm.presenter.fragment.OrderReportPresenter;
import com.holike.crm.util.TimeUtil;
import com.holike.crm.view.fragment.BillView;

import java.util.Date;
import java.util.List;

/**
 * Created by pony on 2019/12/11.
 * 账单汇总
 */
public class BillSummaryFragment extends MyFragment<BillPresenter, BillView> implements BillView {

    private FrameLayout mContainer;
    private String mStartTime, mEndTime;
    private List<Date> mSelectedDates;
    private boolean mThisMonth;

    @Override
    protected BillPresenter attachPresenter() {
        return new BillPresenter();
    }

    @Override
    protected int getLayoutResourceId() {
        return R.layout.fragment_billsummary;
    }

    @Override
    protected void init() {
        setTitle(R.string.bill_summary);
        setRightMenu(getString(R.string.bill_list_detail), view -> openDetailList());
        mContainer = mContentView.findViewById(R.id.fl_container);
        loadThisMonth();
    }

    private void openDetailList() {
        Bundle bundle = new Bundle();
        bundle.putString("startTime", mStartTime);
        bundle.putString("endTime", mEndTime);
        startActivity(BillDetailListActivity.class, bundle);
    }

    private void loadThisMonth() {
        mThisMonth = true;
        mStartTime = null;
        mEndTime = null;
        initData();
    }

    private void initData() {
        showLoading();
        mPresenter.getHeadData(1, 10, mStartTime, mEndTime);
    }

    @Override
    public void onSuccess(BillListBean bean) {
        dismissLoading();
        mContainer.removeAllViews();
        View view = LayoutInflater.from(mContext).inflate(R.layout.include_billsummary_content, mContainer, true);
        Date startDate = TimeUtil.stampToDate(bean.getStartTime());
        Date endDate = TimeUtil.stampToDate(bean.getEndTime());
        String text = TimeUtil.timeMillsFormat(startDate) + "-" + TimeUtil.timeMillsFormat(endDate);
//        String text = TimeUtil.stampToString(bean.getStartTime() + 1, "yyyy.MM.dd") + "-" + TimeUtil.stampToString(bean.getEndTime() + 1, "yyyy.MM.dd");
        TextView tvSelectDate = view.findViewById(R.id.tv_select_date);
        tvSelectDate.setText(text);
        final TextView tvThisMonth = view.findViewById(R.id.tv_this_month);
        tvThisMonth.setVisibility(mThisMonth ? View.INVISIBLE : View.VISIBLE);
        tvSelectDate.setOnClickListener(v -> onSelectDate(tvSelectDate, tvThisMonth));
        tvThisMonth.setOnClickListener(v -> {
            tvThisMonth.setVisibility(View.GONE);
            loadThisMonth();
        });
        BillListBean.HeadDataBean dataBean = bean.getHeadData();
        if (dataBean == null) dataBean = new BillListBean.HeadDataBean();
        ((TextView) view.findViewById(R.id.tv_bill_list_xsrw_1)).setText(textEmptyNumber(dataBean.getZxsrw()));
        ((TextView) view.findViewById(R.id.tv_bill_list_dqrdd_2)).setText(textEmptyNumber(dataBean.getDqrso()));
        ((TextView) view.findViewById(R.id.tv_bill_list_sxed_3)).setText(textEmptyNumber(dataBean.getLimit()));
        ((TextView) view.findViewById(R.id.tv_bill_list_sxye4)).setText(textEmptyNumber(dataBean.getLimitLeft()));
        ((TextView) view.findViewById(R.id.tv_bill_list_qcye5)).setText(textEmptyNumber(dataBean.getZqcye()));
        ((TextView) view.findViewById(R.id.tv_bill_list_crmhk6)).setText(textEmptyNumber(dataBean.getZhuok()));
        ((TextView) view.findViewById(R.id.tv_bill_list_crmflje7)).setText(textEmptyNumber(dataBean.getZflje()));
        ((TextView) view.findViewById(R.id.tv_bill_list_ccjzq8)).setText(textEmptyNumber(dataBean.getCcjzq()));
        ((TextView) view.findViewById(R.id.tv_bill_list_hdzkje9)).setText(textEmptyNumber(dataBean.getHdzkje()));
        ((TextView) view.findViewById(R.id.tv_bill_list_ccjzh10)).setText(textEmptyNumber(dataBean.getCcjzh()));
        ((TextView) view.findViewById(R.id.tv_bill_list_kf11)).setText(textEmptyNumber(dataBean.getKoufei()));
        ((TextView) view.findViewById(R.id.tv_bill_list_kyje12)).setText(textEmptyNumber(dataBean.getZkyje()));
        ((TextView) view.findViewById(R.id.tv_bill_list_kscddje13)).setText(textEmptyNumber(dataBean.getKscsoje()));
        ((TextView) view.findViewById(R.id.tv_bill_list_cwflqcye14)).setText(textEmptyNumber(dataBean.getZflqc()));
        ((TextView) view.findViewById(R.id.tv_bill_list_cwfldqzj15)).setText(textEmptyNumber(dataBean.getZfladd()));
        ((TextView) view.findViewById(R.id.tv_bill_list_cwfldqjs16)).setText(textEmptyNumber(dataBean.getZfldow()));
        ((TextView) view.findViewById(R.id.tv_bill_list_cwflqmye17)).setText(textEmptyNumber(dataBean.getZflqm()));
        ((TextView) view.findViewById(R.id.tv_bill_list_ysqcye18)).setText(textEmptyNumber(dataBean.getZysqc()));
        ((TextView) view.findViewById(R.id.tv_bill_list_ysfsye19)).setText(textEmptyNumber(dataBean.getZysfs()));
        ((TextView) view.findViewById(R.id.tv_bill_list_ysqmye20)).setText(textEmptyNumber(dataBean.getZysqm()));
    }

    private void onSelectDate(final TextView tvDatetime, final TextView tvThisMonth) {
        OrderReportPresenter.selectDate(mContext, mSelectedDates, new CalendarPickerDialog.OnCalendarRangeSelectedListener() {
            @Override
            public void onLeftClicked(CalendarPickerDialog dialog) {
            }

            @Override
            public void onRightClick(CalendarPickerDialog dialog, List<Date> selectedDates, Date start, Date end) {
                dialog.dismiss();
                mSelectedDates = selectedDates;
                if (!selectedDates.isEmpty()) {
                    mStartTime = TimeUtil.dateToStamp(start, false);
                    mEndTime = TimeUtil.dateToStamp(end, true);
                    String text = TimeUtil.timeMillsFormat(mStartTime) + "-" + TimeUtil.timeMillsFormat(mEndTime);
                    tvDatetime.setText(text);
                    mThisMonth = false;
                    tvThisMonth.setVisibility(View.VISIBLE);
                } else {
                    mStartTime = null;
                    mEndTime = null;
                }
                initData();
            }
        });
    }

    @Override
    public void onFailure(String failReason) {
        dismissLoading();
        mContainer.removeAllViews();
        LayoutInflater.from(mContext).inflate(R.layout.include_empty_page, mContainer, true);
        noNetwork(failReason);
    }
}
